package cn.iocoder.yudao.module.mybusiness.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mybusiness.domain.ProductionRecord;
import cn.iocoder.yudao.module.mybusiness.mapper.FacilityMapper;
import cn.iocoder.yudao.module.mybusiness.mapper.RecordMapper;
import com.alibaba.fastjson.JSONObject;
import javafx.util.Pair;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.*;

@Component
public class InitCraft implements InitializingBean {

    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private FacilityMapper facilityMapper;

    private List craftList = new ArrayList();

    private List standardChanceList = new ArrayList();

    @Override
    public void afterPropertiesSet() throws Exception {
        List<String> crafts = facilityMapper.selectCraft();
        craftList.clear();
        standardChanceList.clear();
        crafts.forEach(craft->{
            List<ProductionRecord> records = recordMapper.selectList(new LambdaQueryWrapperX<ProductionRecord>().eq(ProductionRecord::getCraft,craft));
            if(!CollectionUtil.isEmpty(records)){
                MaxEnt maxEnt = new MaxEnt();
                maxEnt.loadData(records,"result");
                maxEnt.train(10);
                List<String> fieldList = new ArrayList<String>();
                fieldList.add(craft); //毕业生
                Pair<String, Double>[] result = maxEnt.predict(fieldList);
                System.out.println(craft+"====="+JSONObject.toJSONString(result));
                Iterator<Pair<String, Double>> iterator = Arrays.stream(result).iterator();
                craftList.add(craft);
                while (iterator.hasNext()){
                    Pair<String, Double> next = iterator.next();
                    String key = next.getKey();
                    Double value = next.getValue();
                    DecimalFormat df = new DecimalFormat("#.##");
                    String formattedValue = df.format(value);
                    double v = Double.valueOf(formattedValue) * 100;
                    if("1".equals(key)){
                        standardChanceList.add(v);
                    }
                }
            }
        });
    }

    public List getCraftList() {
        return craftList;
    }

    public List getStandardChanceList() {
        return standardChanceList;
    }
}
